<?php

namespace app\api\controller;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 微博登录控制器
 */
class  Connectsina extends MobileMall
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        if (config('sina_isuse') != 1){
            ds_json_encode(10001,'系统未开启新浪微博登录功能');//'系统未开启QQ互联功能'
        }
        if (!session('slast_key')){
            ds_json_encode(10001,'系统错误');
        }
    }
    /**
     * 首页
     */
    public function index(){
        /**
         * 检查登录状态
         */
        if(!empty($_COOKIE['key'])){
            $this->bindsina();
        }else {
            $this->autologin();
            $this->register();
        }
    }
    /**
     * 新浪微博账号绑定新用户
     */
    public function register(){
        //实例化模型
        $member_model   = model('member');
        if (request()->isPost()){
            $update_info    = array();
            $update_info['member_password']= md5(trim(input('post.password')));
            $email = input('post.email');
            if(!empty($email)) {
                $update_info['member_email']= $email;
                session('member_email', $email);
            }
            $member_model->editMember(array('member_id'=>session('member_id')),$update_info);
            $this->success(lang('ds_common_save_succ'),HOME_SITE_URL);
        }else{
            //检查登录状态
            //$member_model->checkloginMember();
            //获取新浪微博账号信息
            require_once (PLUGINS_PATH.DS.'login'.DS.'sina_h5'.DS.'saetv2.ex.class.php');
            $c = new SaeTClientV2( config('sina_wb_akey'), config('sina_wb_skey') , session('slast_key.access_token'));
            $sinauser_info = $c->show_user_by_id(session('slast_key.uid'));//根据ID获取用户等基本信息

            
            $logic_connect_api = model('connectapi', 'logic');
            //注册会员信息 返回会员信息
            $reg_info = array(
                'member_sinaopenid'=>session('slast_key.uid'),
                'nickname' => isset($sinauser_info['screen_name'])?$sinauser_info['screen_name']:'',
                'headimgurl' => isset($sinauser_info['avatar_large'])?$sinauser_info['avatar_large']:'',
                
            );
            $wx_member = $logic_connect_api->wx_register($reg_info, 'sina');
            
            
            if($wx_member) {
                $token = $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap');
                $state_data['key'] = $token;
                $state_data['username'] = $wx_member['member_name'];
                $state_data['userid'] = $wx_member['member_id'];
                cookie('user_info', json_encode($this->getMemberUser($wx_member)), 3600 * 24, '/');
                cookie('key', $state_data['key'], 3600 * 24, '/');
                $url = H5_SITE_URL.'/member/index';
                @header("location:$url");
            } else {
                ds_json_encode(10001,'会员注册失败');//"会员注册失败"
            }
        }
    }
    /**
     * 已有用户绑定新浪微博账号
     */
    public function bindsina(){
        $member_model   = model('member');
        //验证新浪账号用户是否已经存在
        $array  = array();
        $array['member_sinaopenid'] = session('slast_key.uid');
        $member_info = $member_model->getMemberInfo($array);
        if (is_array($member_info) && count($member_info)>0){
            session('slast_key.uid',null);
            ds_json_encode(10001,'您的账号已经被管理员禁用，请联系平台客服进行核实');
        }
        //处理sina账号信息
        require_once (PLUGINS_PATH.DS.'login'.DS.'sina_h5'.DS.'saetv2.ex.class.php');
        $c = new \SaeTClientV2( config('sina_wb_akey'), config('sina_wb_skey') , session('slast_key.access_token'));
        $sinauser_info = $c->show_user_by_id(session('slast_key.uid'));//根据ID获取用户等基本信息
        $sina_str = serialize($sinauser_info);
        $edit_state     = $member_model->editMember(array('member_id'=>session('member_id')),array('member_sinaopenid'=>session('slast_key.uid'), 'member_sinainfo'=>$sina_str));
        if ($edit_state){
            ds_json_encode(10000, '','绑定新浪成功');
        }else {
            ds_json_encode(10001,'绑定新浪失败');
        }
    }
    /**
     * 绑定新浪微博账号后自动登录
     */
    public function autologin(){
        //查询是否已经绑定该新浪微博账号,已经绑定则直接跳转
        $member_model   = model('member');
        $array  = array();
        $array['member_sinaopenid'] = session('slast_key.uid');
        $member_info = $member_model->getMemberInfo($array);
        if (is_array($member_info) && count($member_info)>0){
            if(!$member_info['member_state']){//1为启用 0 为禁用
                ds_json_encode(10001,'您的账号已经被管理员禁用,请联系平台客服进行核实');
            }

            $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
            
            $state_data['key'] = $token;
            $state_data['username'] = $member_info['member_name'];
            $state_data['userid'] = $member_info['member_id'];
            cookie('user_info', json_encode($this->getMemberUser($member_info)), 3600 * 24, '/');
            cookie('key', $state_data['key'], 3600 * 24, '/');
            $url = H5_SITE_URL.'/member/index';
            @header("location:$url");exit;
        }
    }

   
}